package health.lm.com.component_base.base.mvp.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import health.lm.com.component_base.base.mvp.inner.IPresent;

/**
 * @author GQI
 * 列表基类，封装刷新和加载更多
 * 基于SmartRefreshLayout和BaseRecyclerViewAdapterHelper封装的
 * @date :2019-06-13 13:50
 */
public abstract class XListActivity<P extends IPresent> extends XActivity<P> implements OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {
    protected int page = 1;
    protected int pageSize = 10;
    protected boolean isRefresh = true;
    protected SmartRefreshLayout rlRefreshLayout;
    protected RecyclerView recyclerView;
    protected BaseQuickAdapter adapter;
    protected boolean isNeedRefresh = false;
    protected int setPreLoadNumber = 3;
    protected int mCurrentCounter = 0;
    protected int mTotalCounter = 0;

    @Override
    public void initData(Bundle savedInstanceState) {
        if (rlRefreshLayout != null) {
            rlRefreshLayout.setOnRefreshListener(this);
            if (isNeedRefresh) {
                rlRefreshLayout.autoRefresh();
            } else {
                loadListData(true, rlRefreshLayout, page, pageSize);
            }
        }
        if (null != adapter) {
            adapter.openLoadAnimation();
        }
    }

    /**
     * 初始化加载更多
     * 不初始化加载更多功能将不会执行
     */
    protected void initLoadMore() {
        if (adapter != null && recyclerView != null) {
            adapter.setOnLoadMoreListener(this, recyclerView);
            // 当列表滑动到倒数第N个Item的时候(默认是1)回调onLoadMoreRequested方法
            adapter.setPreLoadNumber(setPreLoadNumber);
            mCurrentCounter = adapter.getData().size();
        }
    }

    /**
     * 刷新
     *
     * @param refreshLayout 下拉刷新布局
     */
    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        this.page = 1;
        isRefresh = true;
        if (adapter != null) {
            adapter.setEnableLoadMore(false);
        }
        loadListData(isRefresh, rlRefreshLayout, page, pageSize);
    }

    /**
     * 加载更多
     */
    @Override
    public void onLoadMoreRequested() {
        page++;
        isRefresh = false;
        if (adapter == null) {
            return;
        }
        if (adapter.getData().size() < pageSize) {
            //数据全部加载完毕
            adapter.loadMoreEnd(true);
        } else {
            adapter.setEnableLoadMore(true);
            //            if (adapter.getData().size() >= mTotalCounter) {
            //                //数据全部加载完毕
            //                adapter.loadMoreEnd(false);
            //            } else {
            //                adapter.loadMoreComplete();
            //                loadListData(isRefresh, adapter, page, pageSize);
            //            }
            loadListData(isRefresh, rlRefreshLayout, page, pageSize);
        }
    }

    /**
     * 加载数据
     *
     * @param isRefresh true 刷新 false 加载更多
     * @param layout    头部布局
     * @param page      页数
     * @param pageSize  每页数量
     */
    public abstract void loadListData(boolean isRefresh, Object layout, int page, int pageSize);
}
